<template>
	<view>
		<!-- 轮播图，name属性是设置对象属性的图片URL -->
		<u-swiper :list="slides" name="img_url" height="320"></u-swiper>
		<!-- 排序选择 -->
		<u-tabs class="u-text-center u-m-t-30" bar-width="100" item-width="160" :list="sortList" :current="currentSort"
			@change="handleChange"></u-tabs>
		<!-- 商品列表 -->
		<u-row gutter="16" class="u-skeleton">
			<u-col span="6" v-for="goods in goodsList.length !== 0 ? goodsList : [{},{},{},{}]">
				<goods-card :info="goods"></goods-card>
			</u-col>
		</u-row>

		<!-- 引用骨架屏组件 -->
		<u-skeleton :loading="loading" :animation="true" bg-color="#fff"></u-skeleton>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				sortList: [{
						name: '默认'
					},
					{
						name: '销量'
					},
					{
						name: '推荐'
					},
					{
						name: '最新'
					},
				],
				currentSort: 0,
				// 轮播图数据
				slides: [],
				// 商品数据
				goodsList: [],
				page: 1,
				loading: false
			}
		},
		async onLoad() {
			this.getData();
		},
		methods: {
			handleChange(index) {
				this.currentSort = index;
				// 重置商品数据	
				this.goodsList = [];
				this.page = 1;
				this.getData();
			},
			async getData() {
				this.loading = true;

				let params = {
					page: this.page
				};
				const current = this.currentSort;
				// 处理排序
				if (current === 1) params.sales = 1;
				if (current === 2) params.recommend = 1;
				if (current === 3) params.new = 1;
			
				const res = await this.$u.api.index(params);
				console.log(res);
				this.slides = res.slides;
				this.goodsList = [...this.goodsList, ...res.goods.data];
				this.loading = false;
			}
		},
		// 触发到页面底部上拉钩子函数
		onReachBottom() {
			++this.page;
			this.getData();
		}
	}
</script>

<style lang="scss" scoped>

</style>
